diff options
| author | mat <github@matdoes.dev> | 2022-12-15 14:59:56 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-12-15 14:59:56 -0600 |
| commit | 6e723aadf6de45a79b4ef64d288ea275628232c5 (patch) | |
| tree | c27bcc33aefa9212baf6f3d9c2eb1af88d6c5105 /src/routes/player/[player]/[profile]/+page.ts | |
| parent | 89bf3d31e36ad3bdfd45461ee6fb69a4c791f848 (diff) | |
| download | skyblock-stats-6e723aadf6de45a79b4ef64d288ea275628232c5.tar.gz skyblock-stats-6e723aadf6de45a79b4ef64d288ea275628232c5.tar.bz2 skyblock-stats-6e723aadf6de45a79b4ef64d288ea275628232c5.zip | |
start updating to sveltekit v1
Diffstat (limited to 'src/routes/player/[player]/[profile]/+page.ts')
| -rw-r--r-- | src/routes/player/[player]/[profile]/+page.ts | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/routes/player/[player]/[profile]/+page.ts b/src/routes/player/[player]/[profile]/+page.ts new file mode 100644 index 0000000..e97cbfc --- /dev/null +++ b/src/routes/player/[player]/[profile]/+page.ts @@ -0,0 +1,48 @@ +import type { CleanMemberProfile } from '$lib/APITypes' +import type { PageLoad } from './$types' +import { loadPack } from '$lib/packs' +import { fetchApi } from '$lib/api' + +export const load = (async ({ params, fetch, url }) => { + const player: string = params.player + const profile: string = params.profile + const data: CleanMemberProfile = await fetchApi( + `player/${player}/${profile}?customization=true`, + fetch + ).then(async r => { + const text = await r.text() + try { + return JSON.parse(text) + } catch (e) { + throw new Error(`Invalid JSON: ${text}`) + } + }) + + if (!data.member) { + return { + status: 404, + error: 'Unknown profile', + } + } + + if (data.member.username !== player) { + return { + redirect: `/player/${data.member.username}/${data.profile.name}`, + status: 302, + } as any + } + if (!data.member.left && data.profile.name !== profile) { + return { + redirect: `/player/${data.member.username}/${data.profile.name}`, + status: 302, + } as any + } + + const packName = url.searchParams.get('pack') ?? data?.customization?.pack + const pack = await loadPack(packName) + + return { + ...data, + pack, + } +}) satisfies PageLoad |
